﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Web;
using System.Net.Sockets;
using LumenWorks.Framework.IO.Csv;
using System.IO;
using System.Globalization;
using System.Data;
using ICSharpCode.SharpZipLib;
using System.Net.Mime;

namespace Utilities
{
    public class Mail
    {
        private MailMessage email;
        private SmtpClient smtpMail;

        public Mail(string mailSERVER)
        {
            this.email = new MailMessage();
            this.smtpMail = new SmtpClient(mailSERVER, 25);

            this.email.IsBodyHtml = true;
            this.email.From = new MailAddress(Utilities.GlobalConfig.STR_EMAIL_SENDER);
            this.email.Subject = Utilities.GlobalConfig.STR_EMAIL_SUBJECT;
        }

        public void setSender(string str)
        {
            MailAddress maSender = new MailAddress(str);
            this.email.From = maSender;
        }

        public void setRecipient(string str)
        {
            MailAddress maRecipient = new MailAddress(str);
            this.email.To.Add(maRecipient);
        }

        public void setBody(string str)
        {
            this.email.Body = str;
        }

        public void setSubject(string str)
        {
            this.email.Subject = str;
        }

        public void setAttachment(string strFilePath)
        {
            Attachment oAttFile = new Attachment(strFilePath);
            this.email.Attachments.Add(oAttFile);
        }

        public void send()
        {            
            try
            {
                this.smtpMail.Send(this.email);
            }
            catch { }
        }
    }

    public class MailUtil
    {
        public static void sendMail()
        {
            //Add recipients
            string[] listRecipient;
            if (Utilities.GlobalConfig.STR_EMAIL_RECIPIENT.Contains(','))
                listRecipient = Utilities.GlobalConfig.STR_EMAIL_RECIPIENT.Split(',');
            else
                listRecipient = Utilities.GlobalConfig.STR_EMAIL_RECIPIENT.Split(';');
            for (int i = 0; i < listRecipient.Length; i++)
            {
                Utilities.GlobalObject.MAIL.setRecipient(listRecipient[i]);
            }

            //Create zip folder
            Utilities.ZipUtil.zipFiles(Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG, "report.zip", "");
            Utilities.GlobalObject.MAIL.setAttachment(Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + "report.zip");

            //Send
            Utilities.GlobalObject.MAIL.send();
        }
    }
}